﻿using System;
using System.Drawing;
using System.Linq;
using System.Threading;
using JTG.ServiceInterface.InterfaceService;
using Tzw.Common;

namespace JTG.CheckInMachine.Zzzyy
{
    public partial class ShowInfo : BaseForm
    {
        private int _remainTimes = App.GlobSettings.MsgShowTimes; //窗口自动关闭时间
        private IPrinterService _printerService;
        private int _w;
        private int _h;

        public ShowInfo(Enums.ReportStatus status, int w, int h, IPrinterService printerService, string printName) : base(w, h)
        {
            InitializeComponent();

            try
            {
                _printerService = printerService;

                base_lab_time.Visible = false;

                switch (status)
                {
                    case Enums.ReportStatus.Printing:
                    case Enums.ReportStatus.PrintingAndHasReport:
                        var t = new Thread(() =>
                        {
                            try
                            {
                                while (true)
                                {
                                    var printerInfos = _printerService.GetList().FirstOrDefault(x => x.PrinterName == printName);

                                    //打开打印机对应的灯光口
                                    //TODO

                                    //打印完毕后关闭界面
                                    if ((printerInfos == null || printerInfos.Status != 2))
                                    {
                                        //延时关闭界面
                                        if (_remainTimes <= 0)
                                        {
                                            //关闭
                                            Invoke(new Action(CloseWindow));
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        //打印未完成不让关闭界面
                                        _remainTimes = App.GlobSettings.MsgShowTimes;
                                    }

                                    Thread.Sleep(1000);
                                }
                            }
                            catch
                            {
                                // ignored
                            }
                        });
                        t.IsBackground = true;
                        t.Start();

                        MusicPlay.PlayMusic(status == Enums.ReportStatus.Printing
                            ? $"{Environment.CurrentDirectory}\\Voice\\zzzyy\\打印中无额外报告.mp3"
                            : $"{Environment.CurrentDirectory}\\Voice\\zzzyy\\打印机有额外报告.mp3");
                        BackgroundImage = Image.FromFile("Imgs\\zzzyy\\报告单打印中.jpg");
                        break;
                    case Enums.ReportStatus.IsRepeat:
                        BackgroundImage = Image.FromFile("Imgs\\zzzyy\\不能重复打印.jpg");
                        break;
                    case Enums.ReportStatus.NoPaper:
                        BackgroundImage = Image.FromFile("Imgs\\zzzyy\\打印缺纸！.jpg");
                        break;
                    case Enums.ReportStatus.Error:
                        //打印机异常不让继续使用
                        BackgroundImage = Image.FromFile("Imgs\\zzzyy\\打印机故障，请通知工作人员！.jpg");
                        timer1.Enabled = false;
                        new Thread(CheckPrintStatus).Start(_printerService);
                        break;
                    case Enums.ReportStatus.NoReport:
                        MusicPlay.PlayMusic($"{Environment.CurrentDirectory}\\Voice\\zzzyy\\没有您报告单.mp3");
                        BackgroundImage = Image.FromFile("Imgs\\zzzyy\\没有您的报告单.jpg");
                        break;
                    case Enums.ReportStatus.NoResult:
                        MusicPlay.PlayMusic($"{Environment.CurrentDirectory}\\Voice\\zzzyy\\您的报告单正在检验中.mp3");
                        BackgroundImage = Image.FromFile("Imgs\\zzzyy\\结果未出.jpg");
                        break;

                }
            }
            catch (Exception e)
            {
                Logger.Default.Error($"showinfo：{e.Message},{e.StackTrace}");
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _remainTimes--;
            if (_remainTimes <= 0)
            {
                Close();
            }
        }

        private void CloseWindow()
        {
            //关闭灯光
            Close();
        }

        /// <summary>
        /// 每秒检查异常打印机状态
        /// </summary>
        /// <param name="printerService"></param>
        public void CheckPrintStatus(object printerService)
        {
            var ps = (IPrinterService) printerService;

            while (true)
            {
                var printerInfos = ps.GetList();
                if (printerInfos != null && printerInfos.Any())
                {
                    Invoke(new Action(Close));
                    break;
                }
                Thread.Sleep(1000);
            }
        }

    }
}